from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.
from cms import models



class Index(APIView):
    authentication_classes = []
    def get(self,request, *args, **kwargs):
        # 查询分值表中的内容,并正向查找 多找一
        cms_student_sec = models.CMS_SCORE.objects.all()

        from cms.utils.serializers.CMS_STUDENT_serializer import CMS_STUDENT_serializer
        rec_cms_student = CMS_STUDENT_serializer(instance=cms_student_sec,many=True)

        return Response(rec_cms_student.data)


        #知道学校是狮子庙中学,查询中学有多少的人,有哪些部门

        # cms_school = models.CMS_COLLEGE.objects.get(COLLEGE_NAME='狮子庙中学')
        # from cms.utils.serializers.CMS_STUDENT_serializer import CMS_COLLEGE_serializer
        # rec_cms_school = CMS_COLLEGE_serializer(instance=cms_school,many = True)
        # return Response(rec_cms_school.data)

class School(APIView):
    '''查找狮子庙小学的所哟学生'''

    authentication_classes = []
    def get(self,request, *args, **kwargs):
        # 查询学生表

        cms_student_sec = models.CMS_STUDENT.objects.filter(ORG_ID__SCHOOL_ID__COLLEGE_NAME='狮子庙小学').all()
        # cms_student_sec = models.CMS_STUDENT.objects.filter(ORG_ID__SCHOOL_ID__COLLEGE_NAME='狮子庙中学').all()

        from cms.utils.serializers.CMS_STUDENT_serializer import CMS_COLLEGE_serializer
        rec_cms_student = CMS_COLLEGE_serializer(instance=cms_student_sec,many=True)

        return Response(rec_cms_student.data)
    def post(self,request, *args, **kwargs):
        # 判断学校是否存在
        collegeName = request.data.get('collegeName')
        c_obj = models.CMS_COLLEGE.objects.filter(COLLEGE_NAME=collegeName)
        if not c_obj:
            models.CMS_COLLEGE.objects.create(COLLEGE_NAME=collegeName)
            return Response({'statue':'ok'})
        else:
            return Response({'statue':'已存在'})